﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PlayDeezGames.Common;
using System.Xml.Linq;
using System.Collections.Generic;

namespace DungeonDelver.Engine
{
    public class MapColumn : DescriptoredEntityBase
    {
        public class Factory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, System.Xml.Linq.XElement theXElement)
            {
                return new MapColumn(theParent as Map, theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new MapColumn(theParent as Map);
            }
            private Factory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.EngineFactories.MapColumnFactory, new MapColumn.Factory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.EngineFactories.MapColumnFactory);
            }
        }

        private MapColumn(Map parent)
            : base(parent)
        {
        }

        private MapColumn(Map parent, XElement theXElement)
            : base(parent, theXElement)
        {
        }


        public Map Map
        {
            get
            {
                return ParentEntity as Map;
            }
        }
        public Game Game
        {
            get
            {
                return Map != null ? Map.Game : null;
            }
        }

        public int Column
        {
            get
            {
                if (Map != null)
                {
                    foreach (var column in Map.Columns)
                    {
                        if (Map[column] == this)
                        {
                            return column;
                        }
                    }
                }
                throw new IndexOutOfRangeException();
            }
        }

        private Dictionary<int, MapCell> mapCells = new Dictionary<int, MapCell>();

        public Dictionary<int, MapCell>.KeyCollection Rows
        {
            get
            {
                return mapCells.Keys;
            }
        }

        public MapCell this[int index]
        {
            get
            {
                if (mapCells.ContainsKey(index))
                {
                    return mapCells[index];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (mapCells.ContainsKey(index))
                {
                    mapCells[index].ParentEntity = null;
                    mapCells.Remove(index);
                }
                if (value != null)
                {
                    mapCells[index] = value;
                }
            }
        }



        protected override System.Xml.Linq.XElement OnToXElement()
        {
            XElement result = new XElement(Names.EngineXNames.MapColumn);
            result.Add(new XAttribute(PlayDeezGames.Common.Names.CommonXNames.Factory, Names.EngineFactories.MapColumnFactory));
            result.Add(new XAttribute(Names.EngineXNames.Column, Column.ToString()));
            return result;
        }

        protected override void OnFromXElement(XElement theXElement)
        {
            base.OnFromXElement(theXElement);
            Map[int.Parse(theXElement.Attribute(Names.EngineXNames.Column).Value)] = this;
        }

        public static MapColumn Create(Map theParent)
        {
            return EntityFactories.Create(theParent, Names.EngineFactories.MapColumnFactory) as MapColumn;
        }

        public static MapColumn Load(Map theParent, XElement theXElement)
        {
            return EntityFactories.Load(theParent, theXElement) as MapColumn;
        }
    }
}
